package cn.kinoko.common.base.model;

import cn.kinoko.common.code.ErrorCodeEnum;
import cn.kinoko.common.code.SuccessCodeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 前端统一响应实体
 * @author : kk
 */
@Data
@NoArgsConstructor
public class Result<T> {

    private T data;
    private String msg;
    private String code;

    private Result(T data, String msg, String code) {
        this.data = data;
        this.msg = msg;
        this.code = code;
    }

    public static <T> Result<T> error() {
        return new Result<>(null, ErrorCodeEnum.SYSTEM_ERROR_B0001.getDesc(), ErrorCodeEnum.SYSTEM_ERROR_B0001.getCode());
    }

    public static <T> Result<T> error(String msg) {
        return new Result<>(null, msg, ErrorCodeEnum.SYSTEM_ERROR_B0001.getCode());
    }

    public static <T> Result<T> error(String msg, String code) {
        return new Result<>(null, msg, code);
    }

    public static <T> Result<T> error(BaseEnum<?> codeEnum) {
        return new Result<>(null, codeEnum.getDesc(), codeEnum.getCode().toString());
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(data, SuccessCodeEnum.SUCCESS.getDesc(), SuccessCodeEnum.SUCCESS.getCode());
    }

    public static <T> Result<T> success() {
        return new Result<>(null, SuccessCodeEnum.SUCCESS.getDesc(), SuccessCodeEnum.SUCCESS.getCode());
    }

    public static <T> Result<T> toAjax(boolean flag) {
        return flag ? success() : error();
    }

}
